package net.tv90.business.controller;

import net.tv90.business.aspect.annotation.EnumPermission;
import net.tv90.business.aspect.annotation.RequiredPermission;
import net.tv90.business.model.dto.SubjectQueryDTO;
import net.tv90.business.model.dto.SubjectSubmitDTO;
import net.tv90.business.service.SubjectService;
import net.tv90.business.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@CrossOrigin
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    SubjectService subjectService;

    @RequiredPermission(permission = EnumPermission.TEACHER)
    @PostMapping("/submit")
    public Result submitSubject(@RequestBody @Validated SubjectSubmitDTO dto) {
        try {
            subjectService.submitSubject(dto);
        } catch (IllegalArgumentException e) {
            return Result.failed("发布课题失败，" + e.getMessage());
        }
        return Result.success("发布课题成功");
    }

    @RequiredPermission(permission = EnumPermission.ALL)
    @GetMapping("/query")
    public Result querySubject(@Validated SubjectQueryDTO dto) {
        return Result.success(subjectService.querySubject(dto));
    }

    @RequiredPermission(permission = EnumPermission.TEACHER)
    @DeleteMapping("/delete")
    public Result deleteSubject(@RequestParam Long id, @RequestParam Long teacherId) {
        if(subjectService.deleteSubject(id, teacherId)) {
            return Result.success("删除成功");
        }
        return Result.failed("删除失败");
    }

    @RequiredPermission(permission = EnumPermission.STUDENT)
    @PostMapping("/select")
    public Result selectSubject(@RequestParam Long id, @RequestParam Long studentId) {
        try {
            if(subjectService.selectSubject(id, studentId)) {
                return Result.success("选题成功");
            }
            return Result.failed("选题失败");
        } catch (IllegalArgumentException e) {
            return Result.failed(e.getMessage());
        }
    }

    @RequiredPermission(permission = EnumPermission.STUDENT)
    @DeleteMapping("/withdraw")
    public Result withdrawSubject(@RequestParam Long id, @RequestParam Long studentId){
        try {
            if(subjectService.withdrawSubject(id, studentId)) {
                return Result.success("撤销选题成功");
            }
            return Result.failed("撤销选题失败");
        } catch (IllegalArgumentException e){
            return Result.failed(e.getMessage());
        }
    }

    @RequiredPermission(permission = EnumPermission.TEACHER)
    @PostMapping("/confirm")
    public Result confirmSelectStudent(@RequestParam Long id, @RequestParam Long studentId, @RequestParam Long teacherId){
        try {
            if(subjectService.confirmSelectStudent(id, studentId, teacherId)) {
                return Result.success("选择学生成功");
            }
            return Result.failed("选择学生失败");
        } catch (IllegalArgumentException e) {
            return Result.failed(e.getMessage());
        }
    }

}
